/*
 * @LastEditors: 杜康
 * @LastEditTime: 2021-10-01 16:56:07
 */
class Queue {
    constructor () {
        this.count = 0
        this.lowestCount = 0
        this.items = {}
    }
    enqueue (elements) {
        this.items[this.count] = elements
        this.count += 1
    }
    dequeue () {
        if (this.isEmpty()) {
            return undefined
        }
        const result = this.items[this.lowestCount]
        delete this.items[this.lowestCount]
        this.lowestCount += 1
        return result
    }
    peek () {
        if (this.isEmpty()) {
            return undefined
        }
        return this.items[this.lowestCount]
    }
    isEmpty () {
        return this.count - this.lowestCount === 0
    }
    size () {
        return this.count - this.lowestCount
    }
    clear () {
        this.items = {}
        this.lowestCount = 0
        this.count = 0
    }
    toString () {
        if (this.isEmpty()) {
            return ''
        }
        let objString = `${this.items[this.lowestCount]}`
        for (let i = this.lowestCount + 1; i < this.count; i++) {
            objString = `${objString},${this.items[i]}`
        }
        return objString
    }
}
function hotPotato (elementList, num) {
    const queue = new Queue()
    const elimimtatedList = []
    for (let i = 0; i < elementList.length; i++) {
        queue.enqueue(elementList[i])
    }
    while (queue.size() > 1) {
        for (let i = 0; i < num; i++) {
            queue.enqueue(queue.dequeue())
        }
        elimimtatedList.push(queue.dequeue())
    }
    return {
        eliminated: elimimtatedList,
        winner: queue.dequeue()
    }
}

const names = ['Jack', 'John', 'Camila', 'Ingrid', 'Carl']
const result = hotPotato(names, 9)
result.eliminated.forEach(name => {
    console.log(`${name}在击鼓传花游戏中被淘汰`)
})
console.log(`胜利者：${result.winner}`)